package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.db.domain.MarketUserExample;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 付诚
 * @Date 2024/5/19 20:28
 * @Version 1.0
 */
@Service
public class MarketUserServiceImpl implements MarketUserService {
    @Autowired
    MarketUserMapper userMapper;
    @Override
    public List<MarketUser> list(Integer limit, Integer page, String username, String mobile, String sort, String order) {
        MarketUserExample marketUserExample = new MarketUserExample();
        // add_time desc 有sql注入的风险，因为此处接收用户的输入数据了
        marketUserExample.setOrderByClause(sort + " " + order);
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        if(!StringUtils.isEmpty(username)){
            criteria.andUsernameLike("%" + username + "%");
        }
        if(!StringUtils.isEmpty(mobile)){
            criteria.andMobileLike("%" + mobile + "%");
        }

        PageHelper.startPage(page, limit);
        List<MarketUser> marketUsers = userMapper.selectByExampleSelective(marketUserExample, MarketUser.Column.addTime, MarketUser.Column.avatar, MarketUser.Column.deleted, MarketUser.Column.gender, MarketUser.Column.id, MarketUser.Column.lastLoginIp, MarketUser.Column.lastLoginTime,
                MarketUser.Column.mobile, MarketUser.Column.nickname, MarketUser.Column.password, MarketUser.Column.sessionKey, MarketUser.Column.status, MarketUser.Column.updateTime,
                MarketUser.Column.userLevel, MarketUser.Column.username, MarketUser.Column.weixinOpenid);
        return marketUsers;
    }

    @Override
    public MarketUser detail(Integer id) {
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        criteria.andIdEqualTo(id);

        MarketUser marketUser = userMapper.selectOneByExampleSelective(marketUserExample, MarketUser.Column.addTime, MarketUser.Column.avatar, MarketUser.Column.deleted, MarketUser.Column.gender, MarketUser.Column.id, MarketUser.Column.lastLoginIp, MarketUser.Column.lastLoginTime,
                MarketUser.Column.mobile, MarketUser.Column.nickname, MarketUser.Column.password, MarketUser.Column.sessionKey, MarketUser.Column.status, MarketUser.Column.updateTime,
                MarketUser.Column.userLevel, MarketUser.Column.username, MarketUser.Column.weixinOpenid);
        return marketUser;
    }

    @Override
    public void update(MarketUser marketUser) {
        MarketUserExample marketUserExample = new MarketUserExample();
        userMapper.updateByExample(marketUser, marketUserExample);
    }
}
